package com.example.controller;

import com.example.common.Result;
import com.example.entity.CardUse;
import com.example.entity.GridReservation;
import com.example.entity.User;
import com.example.service.CardUseService;
import com.example.service.GridReservationService;
import com.example.service.UserService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
/**
*  位置预订的控制层
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/gridReservation")
public class GridReservationController {

   private final GridReservationService gridReservationService;
    private final UserService userService;
    private final CardUseService cardUseService;

    /**
   * 增
   **/
   @PostMapping("/add")
   public Result add(@RequestBody GridReservation gridReservation) {
       CardUse cardUse = new CardUse();
       if(gridReservation.getType().equals("次数")){
           User user = userService.selectById(TokenUtils.getCurrentUser().getId());
           if(user.getCard() < 1)
               return Result.error("400","次数卡不足");
           user.setCard(user.getCard() - 1);
           userService.updateById(user);

           cardUse.setType("使用用户次数");
       } else if (gridReservation.getType().equals("会员卡")) {
           cardUse.setType("使用会员卡");
           cardUse.setCardId(gridReservation.getCardId());
       }

       cardUseService.add(cardUse);

       gridReservation.setTime(LocalDate.now());
       gridReservation.setUserId(TokenUtils.getCurrentUser().getId());
       gridReservationService.add(gridReservation);
       return Result.success();
   }

   /**
   * 删
   **/
   @DeleteMapping("/delete/{id}")
   public Result deleteById(@PathVariable Integer id) {
       gridReservationService.deleteById(id);
       return Result.success();
   }

   /**
   * 批量删除
   **/
   @DeleteMapping("/delete/batch")
   public Result deleteBatch(@RequestBody List<Integer> ids) {
       gridReservationService.deleteBatch(ids);
       return Result.success();
   }

   /**
   * 改
   **/
   @PutMapping("/update")
   public Result updateById(@RequestBody GridReservation gridReservation) {
       gridReservationService.updateById(gridReservation);
       return Result.success();
   }

   /**
   * 根据ID查询
   **/
   @GetMapping("/selectById/{id}")
   public Result selectById(@PathVariable Integer id) {
       GridReservation gridReservation = gridReservationService.selectById(id);
       return Result.success(gridReservation);
   }

   /**
   * 查询所有
   **/
   @GetMapping("/selectAll")
       public Result selectAll(GridReservation gridReservation ) {
       List<GridReservation> list = gridReservationService.selectAll(gridReservation);
       return Result.success(list);
   }

   /**
   * 分页查询
   **/
   @GetMapping("/selectPage")
   public Result selectPage(GridReservation gridReservation,
   @RequestParam(defaultValue = "1") Integer pageNum,
   @RequestParam(defaultValue = "10") Integer pageSize) {
   PageInfo<GridReservation> page = gridReservationService.selectPage(gridReservation, pageNum, pageSize);
       return Result.success(page);
   }

}